package cn.lili.controller.order;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.Trade;
import cn.lili.modules.order.order.entity.dto.OrderSearchParams;
import cn.lili.modules.order.order.entity.dto.TradeSearchParams;
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
import cn.lili.modules.order.order.entity.vo.OrderSimpleVO;
import cn.lili.modules.order.order.entity.vo.TradeVO;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.modules.order.order.service.TradeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.Objects;

@RestController
@Api(tags = "买家端,交易订单接口")
@RequestMapping("/buyer/trade/trade")
public class TradeController {

    /**
     * 订单
     */
    @Autowired
    private OrderService orderService;

    @Autowired
    private TradeService tradeService;

    @ApiOperation(value = "查询会员交易订单列表")
    @GetMapping
    public ResultMessage<IPage<TradeVO>> queryMineOrder(TradeSearchParams tradeSearchParams) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        tradeSearchParams.setMemberId(currentUser.getId());
        return ResultUtil.data(tradeService.queryByParams(tradeSearchParams));
    }


    @PreventDuplicateSubmissions
    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeSn", value = "交易编号", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "reason", value = "取消原因", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/{tradeSn}/cancel")
    public ResultMessage<Object> cancel(@ApiIgnore @PathVariable String tradeSn, @RequestParam String reason) {
        tradeService.cancel(tradeSn, reason);
        return ResultUtil.success();
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "交易确认收货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "交易编号", required = true, paramType = "path")
    })
    @PostMapping(value = "/{sn}/receiving")
    public ResultMessage<Object> receiving(@NotNull(message = "交易编号不能为空") @PathVariable("sn") String sn) {
        Trade trade = tradeService.getBySn(sn);
        if (trade == null) {
            throw new ServiceException(ResultCode.ORDER_NOT_EXIST);
        }
        //判定是否是待收货状态
        if (!trade.getTradeStatus().equals(OrderStatusEnum.ENTRUCKED.name())) {
            throw new ServiceException(ResultCode.ORDER_DELIVERED_ERROR);
        }
        tradeService.complete(sn);
        return ResultUtil.success();
    }

}
